import 'package:flutter/material.dart';
import 'package:url_launcher/url_launcher.dart';

class HelpCenterPage extends StatelessWidget {
  const HelpCenterPage({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      extendBody: true, // 推荐开启
      backgroundColor: Colors.white,
      appBar: AppBar(
        title: const Text('帮助中心'),
      ),
      body: ListView(
        children: [
          // 常见问题
          _buildSection(
            context,
            title: '常见问题',
            items: [
              const _HelpItem(
                question: '如何修改个人信息？',
                answer: '进入"个人中心"页面，点击"个人信息修改"，即可修改您的头像、昵称、手机号和性别等信息。',
              ),
              const _HelpItem(
                question: '如何切换身份？',
                answer: '如果您是管理员，可以在个人中心页面底部找到"切换身份"按钮，点击后可以选择切换为钓友身份或管理员身份。',
              ),
              const _HelpItem(
                question: '如何查看钓鱼记录？',
                answer: '在个人中心页面的"我的服务"中，点击"钓鱼记录"即可查看您的所有钓鱼记录。',
              ),
            ],
          ),

          // 联系我们
          _buildSection(
            context,
            title: '联系我们',
            contactItems: [
              _ContactItem(
                icon: Icons.phone,
                title: '客服电话',
                content: '400-123-4567',
                onTap: () async {
                  final Uri url = Uri(scheme: 'tel', path: '4001234567');
                  if (await canLaunchUrl(url)) {
                    await launchUrl(url);
                  } else {
                    throw '无法拨打电话 4001234567';
                  }
                },
              ),
              _ContactItem(
                icon: Icons.email,
                title: '客服邮箱',
                content: 'support@fishing.com',
                onTap: () {
                  // TODO: 实现发送邮件功能
                },
              ),
              _ContactItem(
                icon: Icons.wechat,
                title: '微信公众号',
                content: '渔乐圈',
                onTap: () {
                  // TODO: 实现复制微信号功能
                },
              ),
            ],
          ),

          // 关于我们
          _buildSection(
            context,
            title: '关于我们',
            child: const Padding(
              padding: EdgeInsets.all(16),
              child: Text(
                '渔乐圈是一款专注于钓鱼爱好者的社交平台，我们致力于为钓友们提供优质的钓鱼体验和社交服务。在这里，您可以分享钓鱼经验、结交志同道合的朋友、参与各种钓鱼活动。',
                style: TextStyle(
                  fontSize: 14,
                  color: Colors.grey,
                  height: 1.5,
                ),
              ),
            ),
          ),
        ],
      ),
    );
  }

  Widget _buildSection(
    BuildContext context, {
    required String title,
    List<_HelpItem>? items,
    List<_ContactItem>? contactItems,
    Widget? child,
  }) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Padding(
          padding: const EdgeInsets.fromLTRB(16, 16, 16, 8),
          child: Text(
            title,
            style: const TextStyle(
              fontSize: 16,
              fontWeight: FontWeight.bold,
              color: Colors.grey,
            ),
          ),
        ),
        Container(
          margin: const EdgeInsets.symmetric(horizontal: 16),
          decoration: BoxDecoration(
            color: Colors.white,
            borderRadius: BorderRadius.circular(12),
            boxShadow: [
              BoxShadow(
                color: Colors.blue.withAlpha((0.1 * 255).round()),
                blurRadius: 10,
                offset: const Offset(0, 2),
              ),
            ],
          ),
          child: Column(
            children: [
              if (items != null)
                ...items.map((item) => _buildHelpItem(context, item)),
              if (contactItems != null)
                ...contactItems.map((item) => _buildContactItem(context, item)),
              if (child != null) child,
            ],
          ),
        ),
      ],
    );
  }

  Widget _buildHelpItem(BuildContext context, _HelpItem item) {
    return Theme(
      data: Theme.of(context).copyWith(
        dividerColor: Colors.transparent,
        splashColor: Colors.transparent,
        highlightColor: Colors.transparent,
      ),
      child: ExpansionTile(
        backgroundColor: Colors.transparent,
        collapsedBackgroundColor: Colors.transparent,
        title: Text(
          item.question,
          style: const TextStyle(
            fontSize: 16,
            fontWeight: FontWeight.w500,
          ),
        ),
        children: [
          Container(
            decoration: BoxDecoration(
              border: Border(
                bottom: BorderSide(
                  color: Colors.grey.withAlpha((0.1 * 255).round()),
                  width: 1,
                ),
              ),
            ),
            child: Padding(
              padding: const EdgeInsets.fromLTRB(16, 0, 16, 16),
              child: Text(
                item.answer,
                style: const TextStyle(
                  fontSize: 14,
                  color: Colors.grey,
                  height: 1.5,
                ),
              ),
            ),
          ),
        ],
      ),
    );
  }

  Widget _buildContactItem(BuildContext context, _ContactItem item) {
    return InkWell(
      onTap: item.onTap,
      child: Container(
        padding: const EdgeInsets.all(16),
        decoration: BoxDecoration(
          border: Border(
            bottom: BorderSide(
              color: Colors.grey.withAlpha((0.1 * 255).round()),
              width: 1,
            ),
          ),
        ),
        child: Row(
          children: [
            Icon(
              item.icon,
              color: Colors.blue,
              size: 24,
            ),
            const SizedBox(width: 16),
            Expanded(
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Text(
                    item.title,
                    style: const TextStyle(
                      fontSize: 16,
                      fontWeight: FontWeight.w500,
                    ),
                  ),
                  const SizedBox(height: 4),
                  Text(
                    item.content,
                    style: const TextStyle(
                      fontSize: 14,
                      color: Colors.grey,
                    ),
                  ),
                ],
              ),
            ),
            const Icon(
              Icons.chevron_right,
              color: Colors.grey,
            ),
          ],
        ),
      ),
    );
  }
}

class _HelpItem {
  final String question;
  final String answer;

  const _HelpItem({
    required this.question,
    required this.answer,
  });
}

class _ContactItem {
  final IconData icon;
  final String title;
  final String content;
  final VoidCallback onTap;

  const _ContactItem({
    required this.icon,
    required this.title,
    required this.content,
    required this.onTap,
  });
}
